home *** CD-ROM | disk | FTP | other *** search
- // -----------------------------------------------------------------------------
- // File : demoShell.c
- // Date : October 14, 1994
- // Author : somebody, modified by Jim Stout
- // Purpose : A simple shell program to drive demos of "jimsCDEFs" in
- // : both a normal window and a MovableModal dialog.
- // :
- // : see demoDialog.c for the demoCDEF Dialog and,
- // : demoWind.c for routines used for the demoCDEF Window.
- //
- // -----------------------------------------------------------------------------
-
- #include <CommResources.h> // Only needed for System 6 < MacII ROMs
- #include <CTBUtilities.h> // ditto
-
- #include <GestaltEqu.h> // Toolbox
- #include <Traps.h>
-
- #include "demoShell.h" // demo routines
- #include "demoDialog.h"
- #include "demoTab.h"
- #include "demoWind.h"
-
- #include "dialogAssist.h" // utility routines
- #include "movableModal.h"
-
- #include "jimsCDEF.h" // cdef stuff
-
- #define GROWICON false
-
- //=============================================================================
- // demoShell globals
- //=============================================================================
-
- SysEnvRec gSysEnv;
- Boolean gQuit;
- Boolean gInBackground;
-
- short gDemoType;
- ControlHandle gPopCtl, gPopCtl2, gPopCtl3, gBoxCtl,gRad1,gRad2;
- ControlHandle gProgBar1, gProgBar2, gProgBar3;
-
-
- extern pascal void CDmain(); // entry point to CDEF in cdef3D.c
- //=============================================================================
- // demo entry point
- //=============================================================================
-
- main()
- {
- MaxApplZone();
- initToolbox();
- getEvents();
- return(0);
- }
-
- //=============================================================================
- // initialize Mac Toolbox & check for right environment
- //=============================================================================
- void initToolbox()
- {
- Handle menuBar;
- EventRecord thisEvent;
- short count;
-
- gInBackground = FALSE;
- gQuit = FALSE;
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL);
- InitCursor();
-
- if(daGestalt(gestaltCTBVersion) >= 0x0100) {
- InitCRM(); // Only needed for System 6 < MacII ROMs
- InitCTBUtilities(); // ditto
- }
-
- for (count = 1; count <= 3; ++count)
- EventAvail(everyEvent, &thisEvent);
-
- SysEnvirons(curSysEnvVers, &gSysEnv);
-
- if (gSysEnv.machineType < 0)
- doAlert(errNeedMacPlus, true);
-
- if (gSysEnv.systemVersion < 0x0600)
- doAlert(errNeedSys6, true);
-
- if (!trapIsAvailable(_WaitNextEvent))
- doAlert(errNeedMultiF, true);
-
- menuBar = GetNewMBar(rMenuBar);
- if ( menuBar == NIL )
- doAlert(errBadRsrc, true);
-
- SetMenuBar(menuBar);
- DisposHandle(menuBar);
-
- AddResMenu(GetMHandle(mApple), 'DRVR'); // Add DA names to Apple menu
-
- doNewWindow(); // create demo Window
- adjustMenus();
- DrawMenuBar();
- }
-
- //=============================================================================
- // our main event loop. We drive our Progress bars from here.
- //=============================================================================
- void getEvents()
- {
- RgnHandle cursorRgn;
- Boolean gotEvent;
- EventRecord thisEvent;
- long sleep;
-
- if(gSysEnv.systemVersion < 0x0700)
- sleep = 1L; // so progress bars work
- else
- sleep = MAXLONG;
-
- cursorRgn = NIL;
- while ( !gQuit ) {
- gotEvent = WaitNextEvent(everyEvent, &thisEvent, sleep, cursorRgn);
- if ( gotEvent ) {
- doProcessEvents(&thisEvent);
- }
- doPeriodicEvent(&thisEvent);
- }
- }
-
- //=============================================================================
- // process our events
- //=============================================================================
- void doProcessEvents(EventRecord *thisEvent)
- {
- switch ( thisEvent->what ) {
- case mouseDown:
- doMouseDown(thisEvent);
- break;
-
- case keyDown:
- case autoKey:
- doKeyPress(thisEvent);
- break;
-
- case activateEvt:
- doActivate(thisEvent);
- break;
-
- case updateEvt:
- doUpdate(thisEvent);
- break;
-
- case diskEvt:
- diskEvent(thisEvent); // in movableModal
- break;
-
- case osEvt:
- doOSEvent(thisEvent);
- break;
- }
- }
-
- void doPeriodicEvent ( EventRecord * thisEvent )
- {
-
- #pragma unused(thisEvent)
-
- if(isAppWindow(FrontWindow())) {
- if(gDemoType == 6)
- doProgress();
- }
- }
-
- //=============================================================================
- // activate our window
- //=============================================================================
- void doActivate(EventRecord *thisEvent)
- {
- WindowPtr theWindow;
- Boolean becomingActive;
-
- theWindow = (WindowPtr) thisEvent->message;
- becomingActive = (thisEvent->modifiers & activeFlag) != 0;
- #if GROWICON
- if ( isAppWindow(theWindow) ) {
- DrawGrowIcon(theWindow);
- }
- #endif
- }
-
- //=============================================================================
- // doKeyPress
- //=============================================================================
- void doKeyPress(EventRecord *thisEvent)
- {
- char key;
-
- key = thisEvent->message & charCodeMask;
- if ( thisEvent->modifiers & cmdKey ) {
- adjustMenus();
- doMenuCommand(MenuKey(key));
- } else {
- // doKeyPress(thisEvent);
- }
- }
-
- //=============================================================================
- // react to a MouseDown in our window
- //=============================================================================
- void doMouseDown(EventRecord *thisEvent)
- {
- long newSize;
- Rect growRect;
- WindowPtr theWindow;
- short part = FindWindow(thisEvent->where, &theWindow);
-
- switch ( part ) {
- case inMenuBar:
- adjustMenus();
- doMenuCommand(MenuSelect(thisEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(thisEvent, theWindow);
- break;
-
- case inContent:
- if ( theWindow != FrontWindow() )
- SelectWindow(theWindow);
- else
- doContentClick(thisEvent, theWindow);
- break;
-
- case inDrag: // Pass screenBits.bounds to get all gDevices
- DragWindow(theWindow, thisEvent->where, &qd.screenBits.bounds);
- break;
-
- case inGrow:
- growRect = qd.screenBits.bounds;
- growRect.top = growRect.left = 80; // Arbitrary minimum size.
- newSize = GrowWindow(theWindow, thisEvent->where, &growRect);
- if (newSize != 0) {
- invalidateScrollbars(theWindow);
- SizeWindow(theWindow, LOWORD(newSize), HIWORD(newSize), TRUE);
- invalidateScrollbars(theWindow);
- }
- break;
-
- case inGoAway:
- if (TrackGoAway(theWindow, thisEvent->where)) {
- closeAnyWindow(theWindow);
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(theWindow, thisEvent->where, part)) {
- SetPort(theWindow);
- EraseRect(&theWindow->portRect);
- InvalRect(&theWindow->portRect);
- }
- break;
- }
- }
-
- //=============================================================================
- // doContent Click
- //=============================================================================
- void doContentClick ( EventRecord *thisEvent, WindowPtr theWindow)
- {
- Point mousePt;
- short thePart;
- ControlHandle theCtl;
-
- mousePt = thisEvent->where;
- GlobalToLocal(&mousePt);
- thePart = FindControl(mousePt, theWindow, &theCtl);
-
- if(thePart && (theCtl == gPopCtl ||
- theCtl == gPopCtl2 ||
- theCtl == gPopCtl3 ||
- gDemoType == 2)) {
- TrackControl(theCtl, mousePt, (ControlActionUPP)-1);
- if(theCtl == gPopCtl)
- doSwapControls(theWindow);
- else
- if(theCtl == gPopCtl2)
- doChangeColor(theWindow);
- else
- if(theCtl == gPopCtl3)
- doChangePat(theWindow, true);
- }
- else
- if(thePart && theCtl) {
- thePart = TrackControl(theCtl, mousePt, nil);
- if(thePart)
- doControlClick(theCtl, thePart);
- }
-
- }
- //=============================================================================
- // doOSEvent
- //=============================================================================
- void doOSEvent(EventRecord *thisEvent)
- {
- switch ((thisEvent->message >> 24) & 0x00FF) { // High byte of message
- case suspendResumeMessage:
- gInBackground = (thisEvent->message & resumeFlag) == 0;
- #if GROWICON
- if (FrontWindow()) {
- DrawGrowIcon(FrontWindow());
- }
- #endif
- break;
-
- case mouseMovedMessage:
- break;
- }
- }
-
- //=============================================================================
- // doUpdate
- //=============================================================================
- void doUpdate(EventRecord * thisEvent)
- {
- WindowPtr theWindow = (WindowPtr)thisEvent->message;
-
- if ( isAppWindow(theWindow) ) {
- BeginUpdate(theWindow);
- if (!EmptyRgn(theWindow->visRgn)) {
- SetPort(theWindow);
- EraseRgn(theWindow->visRgn);
- DrawControls(theWindow);
- #if GROWICON
- DrawGrowIcon(theWindow);
- #endif
- }
- EndUpdate(theWindow);
- }
- }
-
- //=============================================================================
- // adjustMenus
- //=============================================================================
- void adjustMenus()
- {
- WindowPtr window;
- MenuHandle menu;
-
- window = FrontWindow();
-
- menu = GetMHandle(mFile);
- if ( window != NIL ) {
- EnableItem(menu, iClose);
- }
- else {
- DisableItem(menu, iClose);
- }
- menu = GetMHandle(mEdit);
- if ( isDAWindow(window) ) { // A desk accessory might need the edit menu…
- EnableItem(menu, iUndo);
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- EnableItem(menu, iPaste);
- } else { // … but we don’t use it.
- DisableItem(menu, iUndo);
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- DisableItem(menu, iPaste);
- }
- menu = GetMHandle(mDemo);
- if ( window != NIL ) {
- DisableItem(menu, iDemoWind);
- }
- else {
- EnableItem(menu, iDemoWind);
- }
- }
-
- //=============================================================================
- // disable or enable our menus
- //=============================================================================
- void disableMenus(Boolean disable)
- {
- if(disable) {
- DisableItem(GetMHandle(mFile), 0);
- DisableItem(GetMHandle(mDemo), 0);
- }
- else {
- EnableItem(GetMHandle(mFile), 0);
- EnableItem(GetMHandle(mDemo), 0);
- }
- DrawMenuBar();
- }
- //=============================================================================
- // doMenuCommand
- //=============================================================================
- void doMenuCommand(long menuResult)
- {
- short menuID; // The resource ID of the selected menu
- short menuItem; // The item number of the selected menu
- Str255 daName;
-
- menuID = HIWORD(menuResult);
- menuItem = LOWORD(menuResult);
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout:
- (void) Alert(rAboutAlert, NIL);
- break;
- default: // All non-About items in this menu are DAs
- GetItem(GetMHandle(mApple), menuItem, daName);
- (void) OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem ) {
- case iClose:
- closeAnyWindow(FrontWindow());
- break;
- case iQuit:
- gQuit = TRUE;
- break;
- }
- break;
- case mEdit:
- switch (menuItem) {
- // Call SystemEdit for DA editing & MultiFinder
- // since we don’t do any Editing
- case iUndo:
- case iCut:
- case iCopy:
- case iPaste:
- case iClear:
- (void) SystemEdit(menuItem-1);
- break;
- }
- break;
- case mDemo:
- switch (menuItem) {
- case iDemoWind:
- doNewWindow();
- break;
- case iDemoDlog:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- demoDialog();
-
- disableMenus(false); // bring our menus back
- break;
- case iComparePopup:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- comparePopup();
-
- disableMenus(false); // bring our menus back
- break;
- case iDemoTab1:
- case iDemoTab2:
- case iDemoTab3:
- case iDemoTab4:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- demoTab(menuItem);
-
- disableMenus(false); // bring our menus back
- break;
- case iDynMenu:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- dynMenu();
-
- disableMenus(false); // bring our menus back
- break;
- case iGroupBox:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- demoBoxes();
-
- disableMenus(false); // bring our menus back
- break;
- case iColorButtons:
- HiliteMenu(0);
- disableMenus(true); // shut off our menus
-
- demoButtons();
-
- disableMenus(false); // bring our menus back
- break;
- }
- break;
- }
- HiliteMenu(0); // Unhighlight what MenuSelect or MenuKey hilited
- }
-
- //=============================================================================
- // close a window, regardless of type
- //=============================================================================
- void closeAnyWindow(WindowPtr window)
- {
- if (isDAWindow(window)) {
- CloseDeskAcc( ( (WindowPeek) window )->windowKind );
- }
- else if (isDialogWindow(window)) {
- HideWindow(window);
- }
- else if (isAppWindow(window)) { // has to be our window
- doCloseWindow(window);
- }
- }
-
- //=============================================================================
-
- // doAlert
-
- //=============================================================================
- void doAlert(short errNumber, Boolean exit)
- {
- short itemHit;
- Str255 theMessage;
-
- SetCursor(&qd.arrow);
- GetIndString(theMessage, rErrorStrings, errNumber);
- ParamText(theMessage, NIL, NIL, NIL);
- itemHit = StopAlert(rErrorAlert, NIL);
- if(exit)
- ExitToShell();
- }
-
- //=============================================================================
-
- // isAppWindow
-
- //=============================================================================
- Boolean isAppWindow(WindowPtr window)
- {
- short windowKind;
-
- if ( window == NIL )
- return false;
- else {
- windowKind = ((WindowPeek) window)->windowKind;
- return ((windowKind >= userKind) || (windowKind == dialogKind));
- }
- }
-
- //=============================================================================
-
- // isDAWindow
-
- //=============================================================================
- Boolean isDAWindow(WindowPtr window)
- {
- if ( window == NIL )
- return false;
- else
- return ( ((WindowPeek) window)->windowKind < 0 );
- }
-
- //=============================================================================
-
- // isDialogWindow
-
- //=============================================================================
- Boolean isDialogWindow(WindowPtr window)
- {
- if ( window == NIL )
- return false;
- else
- return ( ((WindowPeek) window)->windowKind == dialogKind );
- }
-
- //=============================================================================
-
- // invalidateScrollbars
-
- //=============================================================================
- void invalidateScrollbars(WindowPtr theWindow)
- {
- Rect tempRect;
-
- SetPort(theWindow);
-
- tempRect = theWindow->portRect;
- tempRect.left = tempRect.right - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
-
- tempRect = theWindow->portRect;
- tempRect.top = tempRect.bottom - 15;
- InvalRect(&tempRect);
- EraseRect(&tempRect);
- }
-
- //=============================================================================
-
- // doNewWindow
-
- //=============================================================================
- WindowPtr doNewWindow()
- {
- WindowPtr theWind;
-
- if(gSysEnv.hasColorQD)
- theWind = GetNewCWindow(kNewWindowID, NIL, (WindowPtr) -1);
- else
- theWind = GetNewWindow(kNewWindowID, NIL, (WindowPtr) -1);
-
- if (theWind != NIL) {
- SetPort(theWind);
- TextFont(geneva);
- TextSize(9);
-
- // Create some controls in the new window
-
- doNewControls(theWind);
- make3DControls(theWind);
- gDemoType = 1;
-
- ShowWindow(theWind);
-
- return(theWind);
- }
- return(0L);
- }
-
-
-
- //=============================================================================
- // Close our window, since from a resource, use DisposeWindow
- //=============================================================================
- void doCloseWindow(WindowPtr theWind)
- {
- DisposeWindow(theWind);
- gDemoType = 0;
- }